package org.corey.share.business;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.corey.share.db.entity.Article;
import org.corey.share.db.entity.ArticleCatogory;
import org.corey.share.db.entity.Link;
import org.corey.share.db.service.ArticleCatogoryService;
import org.corey.share.db.service.ArticleCatogoryServiceImpl;
import org.corey.share.db.service.ArticleService;
import org.corey.share.db.service.ArticleServiceImpl;
import org.corey.share.db.service.AuthService;
import org.corey.share.db.service.AuthServiceImpl;
import org.corey.share.db.service.FriendLinkService;
import org.corey.share.db.service.FriendLinkServiceImpl;
import org.corey.share.misc.Constants;
import org.corey.share.misc.PageData;
import org.corey.share.misc.SystemContext;
import org.corey.share.misc.UserSwitchor;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import freemarker.log.Logger;

@SuppressWarnings("unused")
public class ArticleAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static Logger log = Logger.getLogger(ArticleAction.class.getName());
	private AuthService authService = AuthServiceImpl.getInstance();
	private ArticleService as = new ArticleServiceImpl();
	private FriendLinkService linkService = new FriendLinkServiceImpl();
	private ArticleCatogoryService catogoriesService = new ArticleCatogoryServiceImpl();
	private Article article = new Article();
	private int pageIndex = 1;
	private int pageSize = 3;
	private PageData<Article> articlePageData = new PageData<Article>();
	private Long[] deletedArticles = new Long[0];
	private Long readArticle;
	private List<ArticleCatogory> catogories;
	private Long catogoryId = Constants.ANY_ARTICLE_CATOGORY;
	private List<Link> links = new ArrayList<Link>();

	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public Long[] getDeletedArticles() {
		return deletedArticles;
	}

	public void setDeletedArticles(Long[] deletedArticles) {
		this.deletedArticles = deletedArticles;
	}

	public Long getReadArticle() {
		return readArticle;
	}

	public void setReadArticle(Long readArticle) {
		this.readArticle = readArticle;
	}

	public PageData<Article> getArticlePageData() {
		return articlePageData;
	}

	public void setArticlePageData(PageData<Article> articlePageData) {
		this.articlePageData = articlePageData;
	}

	public List<ArticleCatogory> getCatogories() {
		return catogories;
	}

	public void setCatogories(List<ArticleCatogory> catogories) {
		this.catogories = catogories;
	}

	public Long getCatogoryId() {
		return catogoryId;
	}

	public void setCatogoryId(Long catogoryId) {
		this.catogoryId = catogoryId;
	}

	public List<Link> getLinks() {
		return links;
	}

	public void setLinks(List<Link> links) {
		this.links = links;
	}

	/************************* logic handle methods *************************************/

	public String write() {
		log.debug(article.getTitle());
		Date now = new Date();
		article.setWriteDate(now);
		article.setEditedDate(now);
		article.setIsDeleted(false);
		boolean isSuccess = as.writeArticle(article);
		if (isSuccess) {
			return SUCCESS;
		}
		return INPUT;
	}

	public String readyWrite() {
		String respository = UUID.randomUUID().toString();
		ActionContext.getContext().put("respository", respository);
		this.catogories = catogoriesService.getAllArticleCatogory(UserSwitchor.getSystemOwner(null));
		return SUCCESS;
	}

	public String manage() {
		articlePageData = as.loadAllArticles(pageSize, pageIndex,
				Constants.ANY_ARTICLE_CATOGORY);
		return SUCCESS;
	}

	public String delete() {
		boolean success = as.deleteArticles(this.getDeletedArticles());
		return SUCCESS;
	}

	public String read() {
		links = linkService.getLinksWithPage(0, 0).getPageData();
		articlePageData = as.loadAllArticles(pageSize, pageIndex,
				Constants.ANY_ARTICLE_CATOGORY);
		article = as.loadArticle(readArticle);
		SystemContext.getContext().increaseReadCount(article);
		this.catogories = catogoriesService.getAllArticleCatogory(UserSwitchor.getSystemOwner(null));
		return SUCCESS;
	}

	public String displayArticlesWithPageByCatogory() {
		links = linkService.getLinksWithPage(0, 0).getPageData();
		articlePageData = as.loadAllArticles(pageSize, pageIndex,
				this.catogoryId);
		this.catogories = catogoriesService.getAllArticleCatogory(UserSwitchor.getSystemOwner(null));
		return SUCCESS;
	}
}
